// pages/jianzhi/jianzhi.js
const db = wx.cloud.database().collection("can")
Page({
  data: {
    listData: []
  },
  onShow: function () {
    this.getData ()
  },
  getData () { // 获取数据
    let thiss = this
    db.get({
      success (res) {
        thiss.setData({
          listData:res.data
        })
      }
    })
  },
  handleadd (data) {
    let list = this.data.listData
    list [data.target.dataset.index].num++
    this.setData({
      listData:list
    })
  },
  handlejian (data) {
    let list = this.data.listData
    if(list [data.target.dataset.index].num === 0) {
      console.log('')
    }else{
      list [data.target.dataset.index].num--
    }
    this.setData({
      listData:list
    })
  },
  Time(){
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;
    //获取当前时间
    var n = timestamp * 1000;
    var date = new Date(n);
    var Y = date.getFullYear();
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    var h = date.getHours();
    var m = date.getMinutes();
    var zong=Y+"年"+M+"月"+D+"日"+h+":"+m
    return zong
  },
  jiesuan (data) {
    const dbs = wx.cloud.database().collection("candan")
    wx.showLoading({
      title: "排队中...." ,
      mask: true,
    })
    dbs.add({
      data: {
        qian:this.data.listData[data.target.dataset.index].qian*this.data.listData[data.target.dataset.index].num,
        canname:this.data.listData[data.target.dataset.index].name,
        username:wx.getStorageSync("userInfo").nickName,
        time:this.Time()
      },
      success (res) {
        wx.showToast({
          title: '购买成功...',
          icon: 'none'
        })
        wx.hideLoading();
        console.log(res)
      },
      fail (err) {
        console.log(err)
      }
    })
  }
})